package com.meiyou.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alipay.api.internal.util.StringUtils;
import com.meiyou.pojo.Bgadmin;
import com.meiyou.pojo.vo.BgadminVo;
import com.meiyou.pojo.vo.UserVo;
import com.meiyou.service.BgadminService;
import com.meiyou.utils.IdGenerator;
import com.meiyou.utils.MD5UtilVo;
import com.meiyou.utils.Msg;
import com.meiyou.utils.PagesMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "管理员接口", tags = { "管理员接口" })
@RequestMapping("/admin")
public class AdminController extends BasicController{

	@Autowired
	BgadminService  bgadminService;
	
	//登录
	@RequestMapping(value="/loginAdmin",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="管理员登录",notes="管理员登录",httpMethod="POST")
	public Msg login(String username,String password) throws IllegalAccessException, InvocationTargetException {	
		if(StringUtils.isEmpty(username) && StringUtils.isEmpty(password)) {
			return Msg.fail();
		}
		Bgadmin bgadmin = new Bgadmin();
		password = MD5UtilVo.md5(password);
		System.out.println(password); 
		bgadmin.setaCount(username);
		bgadmin.setaPwd(password); 
		List<Bgadmin> list = bgadminService.login(bgadmin);
		System.out.println(list.size());
		if(list!=null && list.size()==1) {
			//把token传过去
			BgadminVo bgadminVo = setRedisSession(list.get(0));  
			return Msg.success().add("bgadminVo", bgadminVo);
		}else {
			return Msg.fail(); 
		} 
	}
	
	//注册
	@RequestMapping(value="/register",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="管理员注册",notes="管理员注册",httpMethod="POST")
	public Msg register(String username,String password) {
		if(StringUtils.isEmpty(username) && StringUtils.isEmpty(password)) {
			return Msg.fail();
		}
		password = MD5UtilVo.md5(password);
		Bgadmin bgadmin = new Bgadmin();
		bgadmin.setaCount(username);
		bgadmin.setaPwd(password); 
		bgadminService.insertAdmin(bgadmin);
		return Msg.success();  
	}
	
	//退出
	@RequestMapping(value="/exit",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="管理员退出",notes="管理员退出",httpMethod="POST")
	public Msg exit(String id) {
		System.out.println(id);
		jedisClientPool.del(USER_REDIS_SESSION+":"+id);
		return Msg.success();  
	}
	
	
	//查询所有用户信息
	@RequestMapping(value="/selectuserInfo",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="查询所有用户信息",notes="查询所有用户信息",httpMethod="POST")
	public Msg selectuserInfo(int pn,UserVo userVo) {
		//利用list截取来进行分页
		String strtime = userVo.getStrtime();
		if(strtime!=null && strtime!="") {   
			String s[] = strtime.split(",");
		    userVo.setStarttime(s[0]);
		    userVo.setEndtime(s[1]);
		}
		System.out.println(userVo);
		List<UserVo> list = bgadminService.selectAllInfo(userVo);
		
		Map<String,Object> map = PagesMap.getpagesUserMaps(list, pn); 
		return Msg.success().add("pageInfo", map);  
	}
	
	//查询
	//创建redis-session方法
	public BgadminVo setRedisSession(Bgadmin bgadmin) throws IllegalAccessException, InvocationTargetException {
		String uniqueToken = IdGenerator.get();
		jedisClientPool.setValueTime(USER_REDIS_SESSION+":"+bgadmin.getaId(),uniqueToken,1*60*10);
		BgadminVo bgadminVo = new BgadminVo();
		BeanUtils.copyProperties(bgadminVo, bgadmin);
		bgadminVo.setToken(uniqueToken);
		return bgadminVo; 
	}
}
